/**----------------------------------------
 * @description 游戏脚本入口
 * @author      lxc
 * @file        GameEntrance.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */
import { GlobalMgr } from "./base/GlobalMgr";
import { MsgComponent } from "./base/message/MsgComponent";

const {ccclass, property} = cc._decorator;

/** 开启动态合图，不清理缓存 */
cc.macro.CLEANUP_IMAGE_CACHE = false;

cc.dynamicAtlasManager.enabled = true;

export const enum GameMoudle{
    LayerManager = "layers"
}

@ccclass
export class GameEntrance extends MsgComponent {

    private needInitModule : GameMoudle[] = [
        GameMoudle.LayerManager,
    ]

    protected onLoad(): void {
        super.onLoad()
        /** 屏蔽多点触摸 */
        cc.macro.ENABLE_MULTI_TOUCH = false;

        if (typeof gVar === "undefined"){
            new GlobalMgr()
        }
        this.awaysPrint("GameVersion:1.0.0")
        this.initSystemEvent()
    }
    /**
     * 初始化系统事件监听
     */
    initSystemEvent(){
        cc.game.on(cc.game.EVENT_SHOW,()=>{
            this.printLog("EVENT_SHOW")
        })

        cc.game.on(cc.game.EVENT_HIDE,()=>{
            this.printLog("EVENT_HIDE")
        })
    }

    protected onDisable(): void {
        super.onDisable()
        cc.game.off(cc.game.EVENT_HIDE);
        cc.game.off(cc.game.EVENT_SHOW);
    }

    initListeners(): void {
        super.initListeners()
        this.addUIListener(LocalMsg.ModuleInitDone, this.moduleInitDone.bind(this))
    }
    /**
     * 模块初始化完成
     */
    moduleInitDone(module : GameMoudle){
        let index = this.needInitModule.indexOf(module)
        if(this.needInitModule.length > 0 && index >= 0){
            this.needInitModule.splice(index,1);
            if(this.needInitModule.length <= 0){
                gVar.UIMsgMgr.dispatchMsg(LocalMsg.AllModulesInitDone)
            }
        }
    }
    
    gameExit(){
        cc.game.end()
    }
}
